﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.ApproveCommand;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.ApproveCommandHandler
{
    /// <summary>
    /// 反填请假申请信息-命令
    /// </summary>
    public class FanTianLeaveModelCommandHandler : IRequestHandler<FanTianLeaveModelCommand, APIResult<LeaveModel>>
    {

        private readonly IBaseRepository<LeaveModel> leaveRepos;

        public FanTianLeaveModelCommandHandler(IBaseRepository<LeaveModel> leaveRepos)
        {
            this.leaveRepos = leaveRepos;
        }

        public async Task<APIResult<LeaveModel>> Handle(FanTianLeaveModelCommand request, CancellationToken cancellationToken)
        {
            APIResult<LeaveModel> result = new APIResult<LeaveModel>();
            var list = await leaveRepos.GetAll().Result.FirstOrDefaultAsync(x => x.LeaveId == request.LeaveId);
            //失败情况
            if (list == null)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
                result.data = list;
            }
            else
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = list;
            }
            return await Task.FromResult(result);
        }
    }
}
